﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using WebClient;
namespace WebClient.admin.Modules.users
{
    public partial class Edit : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.btnAddNew.Attributes.Add("onclick", "return OnSubmitClick();");
            if (!this.IsPostBack)
            {
                LoadGroup();
                InitItem();
            }
        }

        private void LoadGroup()
        {
            WebData db = new WebData();
            this.cbbgroupId.DataSource = db.GetAllUsergroups();
            this.cbbgroupId.DataTextField = "groupName";
            this.cbbgroupId.DataValueField = "id";
            this.cbbgroupId.DataBind();
        }

        private void AddNew()
        {
            WebData data = new WebData();
            Users item = new Users();
            int groupId = 2;
            try
            {
                groupId = Convert.ToInt32(cbbgroupId.SelectedValue);
            }
            catch { }
            long id = 1;
            try
            {
                id = Convert.ToInt64(Request.Params["ID1"]);
            }
            catch { }
            item.id = id;
            item.userName = this.txtuserName.Text.Trim();
            item.fullName = this.txtfullName.Text.Trim();
            item.password = this.txtpassword.Text.Trim();
            item.email = this.txtemail.Text.Trim();
            item.create_on = DateTime.Now;
            item.lastDate = DateTime.Now;
            item.status = 2;
            item.passDate = DateTime.Now;
            item.address = this.txtaddress.Text.Trim();
            item.phone = this.txtphone.Text.Trim();
            item.isActive = 1;
            item.groupID = groupId;
            item.birthDay = DateTime.Now;
            data.OnSubmitUsers(item, 1);
        }
        private void InitItem()
        {
            ViewState["URLReferrer"] = Request.UrlReferrer.AbsoluteUri;
            WebData db = new WebData();
            Users item = new Users();
            long id = 1;
            try {
                id = Convert.ToInt64(Request.Params["ID1"]);
            }
            catch { }
            item = db.GetItemByIdUsers(id);
            if (item != null)
            {
                
                txtuserName.Text = item.userName.ToString();
                txtfullName.Text = item.fullName.ToString();
                txtpassword.Text = item.password.ToString();
                txtemail.Text = item.email.ToString();                
                txtaddress.Text = item.address.ToString();
                txtphone.Text = item.phone.ToString();
                cbbgroupId.SelectedValue = item.groupID.ToString();
                txtpassword.Text = item.password;
            }

        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            AddNew();
        }
    }
}